home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / amhelios.cpp < prev    next >
C/C++ Source or Header  |  1999-01-01  |  9KB  |  324 lines

  1. /* -------------------------------------------------------------------------- *\
  2.    AMHELIOS.CPP
  3.    Copyright © 1997 by Jarno van der Linden
  4.    jarno@kcbbs.gen.nz
  5.  
  6.    Radiosity renderer based on Helios. Original copyright notice:
  7.  
  8. //  Author:     Ian Ashdown, P.Eng.
  9. //              byHeart Software Limited
  10. //              620 Ballantree Road
  11. //              West Vancouver, B.C.
  12. //              Canada V7S 1W3
  13. //              Tel. (604) 922-6148
  14. //              Fax. (604) 987-7621
  15. //
  16. //  Copyright 1994-1996 byHeart Software Limited
  17. //
  18. //  The following source code has been derived from:
  19. //
  20. //    Ashdown, I. 1994. Radiosity: A Programmer's
  21. //    Perspective. New York, NY: John Wiley & Sons.
  22. //
  23. //  It may be freely copied, redistributed, and/or modified
  24. //  for personal use ONLY, as long as the copyright notice
  25. //  is included with all source code files.
  26.  
  27.    02 Aug 1997: Started GUI
  28. \* -------------------------------------------------------------------------- */
  29.  
  30. /*
  31. mkmk target=Amhelios force Amhelios.cpp error.cpp parse.cpp ff_clip.cpp ct_delta.cpp hc_scan.cpp ff_scan.cpp ct_scan.cpp patch3.cpp hc_delta.cpp environ.cpp ct_clip.cpp cubic_t.cpp prog_rad.cpp rad_eqn.cpp syn_cam.cpp bitmap24.cpp p_render.cpp c_jitter.cpp view_sys.cpp gamma.cpp p_clip4.cpp vector3.cpp MUIpreview.cpp CalcSub.cpp
  32. */
  33.  
  34. /* -------------------------------- Includes -------------------------------- */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <time.h>
  38. #include "error.h"
  39. #include "parse.h"
  40. #include "syn_cam.h"
  41.  
  42. #if (defined(_HEMI_CUBE) || defined(_CUBIC_TETRA))
  43. #include "prog_rad.h"
  44. #elif defined(_RAY_CAST)
  45. #include "ray_rad.h"
  46. #else
  47. #include "rad_eqn.h"
  48. #endif
  49.  
  50. #include <proto/intuition.h>
  51. #include <proto/exec.h>
  52.  
  53. #include <libraries/mui.h>
  54. #include <proto/muimaster.h>
  55.  
  56. #include "MUIpreview.h"
  57. #include "CalcSub.h"
  58.  
  59. /* ------------------------------ Definitions ------------------------------- */
  60. #define PROGRAM "AmHelios"
  61.  
  62. /* --------------------------------- Macros --------------------------------- */
  63.  
  64. /* -------------------------------- Typedefs -------------------------------- */
  65.  
  66. /* ------------------------------ Proto Types ------------------------------- */
  67. ULONG iget(APTR obj,Tag attr);
  68. void set(APTR obj,Tag attr,ULONG v);
  69.  
  70. int main( int argc, char **argv );
  71. void Quit(char *str);
  72.  
  73. void OpenLibraries(void);
  74. void CloseLibraries(void);
  75. void OpenDisplay(void);
  76. void CloseDisplay(void);
  77.  
  78. void MainWait(void);
  79.  
  80. void GetWorld(char *file,char *dir);
  81.  
  82. __saveds void Render(void);
  83.  
  84. /* -------------------------------- Structs --------------------------------- */
  85.  
  86. /* -------------------------------- Globals --------------------------------- */
  87. static Parse Parser;            // World file parser
  88. static Environ Environment;     // Environment
  89.  
  90. static SynCamera PreviewCamera(256, 256, -180.0, 90.0, 0.0, 0.0);
  91. static SynCamera RenderCamera(704, 564, -180.0, 90.0, 0.0, 0.0);
  92. static WinBitmap Bitmap;
  93.  
  94. // Radiosity equation solver
  95. #if (defined(_HEMI_CUBE) || defined(_CUBIC_TETRA))
  96. static ProgRad Radiosity;       // Progressive radiosity
  97. #elif defined(_RAY_CAST)
  98. static RayRad Radiosity;        // Ray cast radiosity
  99. #else
  100. static RadEqnSolve Radiosity;   // Dummy equation solver
  101. #endif
  102.  
  103. // MUI bits
  104. struct Library *MUIMasterBase = NULL;
  105. APTR my_app;
  106. APTR my_window;
  107. APTR mui_x, mui_y, mui_z, mui_eye, mui_focus;
  108. APTR mui_detail;
  109. APTR mui_render;
  110. APTR mui_preview;
  111.  
  112. CalcSub *calcsub;
  113.  
  114. /* ---------------------------------- Code ---------------------------------- */
  115. ULONG iget(APTR obj,Tag attr)
  116. {
  117.     ULONG v;
  118.  
  119.     GetAttr(attr,obj,&v);
  120.  
  121.     return v;
  122. }
  123.  
  124.  
  125. void set(APTR obj,Tag attr,ULONG v)
  126. {
  127.     SetAttrs(obj,attr,v,TAG_DONE);
  128. }
  129.  
  130.  
  131.  
  132.  
  133. int main( int argc, char **argv )
  134. {
  135.     srand((unsigned) time(NULL));
  136.  
  137.     GetWorld(argc > 1 ? argv[1] : "demo/room.wld", argc > 2 ? argv[2] : "demo");
  138.  
  139.     OpenLibraries();
  140.     OpenDisplay();
  141.  
  142.     MainWait();
  143.  
  144.     Quit(NULL);
  145.  
  146.     return 0;
  147. }
  148.  
  149.  
  150. void Quit(char *str)
  151. {
  152.     // Report the error message (if any)
  153.     if(str!=NULL)
  154.         printf(PROGRAM" Error: %s!\n",str);
  155.  
  156.     CloseDisplay();
  157.     CloseLibraries();
  158.  
  159.  
  160.     exit(0);
  161. }
  162.  
  163.  
  164.  
  165. void OpenLibraries(void)
  166. {
  167.     MUIMasterBase = OpenLibrary((TEXT *)"muimaster.library",0L);
  168.     if(!MUIMasterBase)
  169.         Quit("Couldn't open muimaster.library");
  170. }
  171.  
  172.  
  173. void CloseLibraries(void)
  174. {
  175.     if(MUIMasterBase) CloseLibrary(MUIMasterBase);
  176.     MUIMasterBase = NULL;
  177. }
  178.  
  179.  
  180.  
  181.  
  182. void OpenDisplay(void)
  183. {
  184.     static struct Hook RenderHook={ {NULL,NULL},(__stdargs ULONG (*)())Render,NULL,NULL };
  185.     static const char *CYA_Detail[] = { "Global box", "Object boxes", "Full wireframe", NULL };
  186.  
  187.     if(!InitPreviewClass())
  188.         Quit("Couldn't create custom MUI class Preview");
  189.  
  190.     my_app = ApplicationObject,
  191.         MUIA_Application_Title,            "AmHelios",
  192.         MUIA_Application_Author,        "Jarno van der Linden",
  193.         MUIA_Application_Base,            "AMHELIOS",
  194.         MUIA_Application_Copyright,        "Jarno van der Linden",
  195.         MUIA_Application_Description,    "Radiosity renderer based on Helios",
  196.         MUIA_Application_Version,        "$VER: AmHelios 0.1 (2.8.97)",
  197.         SubWindow, my_window = WindowObject,
  198.             MUIA_Window_Title,    "AmHelios",
  199.             MUIA_Window_ID,        10,
  200.             WindowContents, HGroup,
  201.                 Child, VGroup,
  202.                     Child, ColGroup(2),
  203.                         Child, KeyLabel2("X:",'x'), Child, mui_x = KeyCheckMark(TRUE,'x'),
  204.                         Child, KeyLabel2("Y:",'y'), Child, mui_y = KeyCheckMark(TRUE,'y'),
  205.                         Child, KeyLabel2("Z:",'z'), Child, mui_z = KeyCheckMark(FALSE,'z'),
  206.                         Child, KeyLabel2("Eye:",'e'), Child, mui_eye = KeyCheckMark(TRUE,'e'),
  207.                         Child, KeyLabel2("Focus:",'f'), Child, mui_focus = KeyCheckMark(FALSE,'f'),
  208.                         Child, KeyLabel2("Detail:",'d'), Child, mui_detail = KeyCycle(CYA_Detail, 'd'),
  209.                         End,
  210.                     End,
  211.                 Child, mui_preview = MUIPreview(&Environment,&PreviewCamera),
  212.                     MUIA_Preview_Mode, PREVIEW_MOVEEYE | PREVIEW_MOVEX | PREVIEW_MOVEY,
  213.                     MUIA_Preview_Level, 0,
  214.                     TextFrame,
  215.                     End,
  216.                 Child, VGroup,
  217.                     Child, mui_render = KeyButton("Render",'r'),
  218.                     End,
  219.                 End,
  220.             End,
  221.         End;
  222.  
  223.     if(!my_app)
  224.         Quit("Couldn't create application");
  225.  
  226.     DoMethod((Object *)mui_x,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,
  227.         mui_preview,3,MUIM_Set,MUIA_Preview_Mode_Toggle,PREVIEW_MOVEX);
  228.     DoMethod((Object *)mui_y,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,
  229.         mui_preview,3,MUIM_Set,MUIA_Preview_Mode_Toggle,PREVIEW_MOVEY);
  230.     DoMethod((Object *)mui_z,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,
  231.         mui_preview,3,MUIM_Set,MUIA_Preview_Mode_Toggle,PREVIEW_MOVEZ);
  232.     DoMethod((Object *)mui_eye,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,
  233.         mui_preview,3,MUIM_Set,MUIA_Preview_Mode_Toggle,PREVIEW_MOVEEYE);
  234.     DoMethod((Object *)mui_focus,MUIM_Notify,MUIA_Selected,MUIV_EveryTime,
  235.         mui_preview,3,MUIM_Set,MUIA_Preview_Mode_Toggle,PREVIEW_MOVEFOCUS);
  236.  
  237.     DoMethod((Object *)mui_detail,MUIM_Notify,MUIA_Cycle_Active,MUIV_EveryTime,
  238.         mui_preview,3,MUIM_Set,MUIA_Preview_Level,MUIV_TriggerValue);
  239.  
  240.     DoMethod((Object *)mui_render,MUIM_Notify,MUIA_Pressed,FALSE,
  241.         mui_render,2,MUIM_CallHook,&RenderHook);
  242.  
  243.     DoMethod((Object *)my_window,MUIM_Notify,MUIA_Window_CloseRequest,TRUE,
  244.         my_app,2,MUIM_Application_ReturnID,MUIV_Application_ReturnID_Quit);
  245.  
  246.     set(my_window,MUIA_Window_Open,TRUE);
  247. }
  248.  
  249.  
  250. void CloseDisplay(void)
  251. {
  252.     if(my_app) MUI_DisposeObject((Object *)my_app);
  253.     my_app = NULL;
  254.  
  255.     DeletePreviewClass();
  256. }
  257.  
  258.  
  259. void MainWait(void)
  260. {
  261.     ULONG sigs = 0;
  262.  
  263.     if (Radiosity.GetStatus() == FALSE)
  264.     {
  265.         OutOfMemory();
  266.         return;
  267.     }
  268.  
  269.     Radiosity.SetStopCriterion(0.01);
  270.     Radiosity.SetMaxStep(INT_MAX);
  271.     Radiosity.EnableAmbient();
  272.  
  273.     calcsub = new CalcSub("CalcSub",&Radiosity,&Environment);
  274.     calcsub->Start();
  275.  
  276.     while(DoMethod((Object *)my_app,MUIM_Application_NewInput,&sigs) != MUIV_Application_ReturnID_Quit)
  277.     {
  278.         if(sigs)
  279.         {
  280.             sigs = Wait(sigs | SIGBREAKF_CTRL_C);
  281.             if(sigs & SIGBREAKF_CTRL_C)
  282.                 break;
  283.  
  284.  
  285.         }
  286.         printf("%d %lf %s\n", Radiosity.GetStepCount(),
  287.                               Radiosity.GetConvergence(),
  288.                               Radiosity.GetElapsedTime());
  289.  
  290.     }
  291.  
  292.     delete calcsub;
  293. }
  294.  
  295.  
  296. void GetWorld(char *file,char *dir)
  297. {
  298.     if(Parser.ParseFile(file, dir, &Environment) == FALSE)
  299.         Quit("Failed to parse input file");
  300.  
  301.     PreviewCamera.InitViewSystem(&Environment);
  302.     RenderCamera.InitViewSystem(&Environment);
  303. }
  304.  
  305.  
  306. void Render(void)
  307. {
  308.     PreviewCamera.CopyViewSys(RenderCamera);
  309.     if(Bitmap.Open(RenderCamera.GetWidth(), RenderCamera.GetHeight()) == TRUE)
  310.     {
  311.         BOOL repeat;
  312.  
  313.         calcsub->Snapshot();
  314.  
  315.         do {
  316.             RenderCamera.Shoot(&Environment, &Bitmap, &repeat);
  317.         } while(repeat == TRUE);
  318.  
  319.         Bitmap.Write("T:amhelios.iff24");
  320.     }
  321.  
  322.     Bitmap.Close();
  323. }
  324.